package com.example.model.service.impl;

import com.example.model.mapper.UserMapper;
import com.example.model.pojo.User;
import com.example.model.service.UserService;
import com.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.utils.Md5Util;

import java.time.LocalDateTime;
import java.util.Map;

//接口的实现
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findById(Integer id) {
        //调用mapper里面那个找id的函数
        return userMapper.findById(id);
    }
    @Override
    public User findByUserName(String username) {
        User u = userMapper.findByUserName(username);
        return u;
    }

    @Override
    public void register(String username, String password1) {
        //加密
        String password = Md5Util.getMD5String(password1);
        //添加
        userMapper.add(username,password);
    }
    @Override
    public void update(User user){
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl){
        //接口没有叫传id，从线程里面去拿
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        String password = Md5Util.getMD5String(newPwd);
        userMapper.updatePwd(password, id);
    }
}
